package com.thread;

public class test20 {
    public static void main(String[] args) {
        GuardedObject object=new GuardedObject();
        new Thread(()->{
            Object o = object.get();
            System.out.println(o);
        },"t1").start();

        new Thread(()->{
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            object.complete(5);
        },"t1").start();

    }
}

class GuardedObject{
    private Object response;
    public Object get(){
        synchronized (this){
            try {
                while(response==null){
                    this.wait();
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return  response;
        }
    }


    public void complete(Object response){
        synchronized (this){
            this.response=response;
            this.notifyAll();
        }
    }
}
